﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace Room
{
    public class RCUIBox : RCUIBase
    {
        public RCUIBox(RoomCharactor owner)
            : base(owner)
        {
            this.owner = owner;
            type = UIType.Box;
        }

        string boxId;

        public void Init(string boxId)
        {
            this.boxId = boxId;
        }

        protected override void OnClick()
        {
            OpenBox();
        }

        private void OpenBox()
        {
            foreach(var b in StaticData.boxs)
            {
                if (b.isOpen != 0 && b.boxId == boxId) return;
            } 

            CSCopyBox box = new CSCopyBox();
            box.boxId = this.boxId;
            NetAPI.SendHttp(OpDefine.CSCopyBox, box, (o, ok) =>
            {
                if (ok)
                {
                    SCCopyBox b = o.kBody as SCCopyBox;
                    
                    owner.PlayAnim("process");

                    foreach(var inBox in StaticData.boxs)
                    {
                        if(box.boxId == inBox.boxId)
                        {
                            inBox.isOpen = 1;
                        }
                    }


                    string path = "particles/" + Dict.Blo.DictParticleBlo.GetCommonParticle("13").ResourcePath;//@"particles/castle_obj_706/castle_obj_706_process";
                    AssetLoadHelper.Instance.LoadAssetAsync(path, CommonMethod.GetAssetNameFromAssetBundleName(path), typeof(GameObject),
                    (asset) =>
                    {
                        GameObject obj = asset as GameObject;
                        if (obj != null)
                        {
                            if (owner == null)
                            {
                                GameObject.Destroy(obj);
                                return;
                            }

                            obj.SetActive(true);
                            obj.transform.SetParent(owner.transform);
                            obj.transform.localPosition = Vector3.zero;
                            obj.transform.localScale = Vector3.one;
                            obj.transform.localRotation = Quaternion.Euler(Vector3.zero);

                            GameObject.Destroy(obj, 2);
                        }
                    });

                    /*
                    var temp = Resources.Load<GameObject>(@"particles\castle_obj_706\castle_obj_706_process");
                    var inst = GameObject.Instantiate(temp);
                    inst.transform.SetParent(owner.transform);
                    inst.transform.localScale = Vector3.one;
                    inst.transform.localPosition = Vector3.zero;
                    */

                    var ai = owner.ai as RCAIBox;
                    ai.Init(boxId, 1);

					
					AudioManager.Instance.PlayUIAudio("openChest");

                    var page = UI.PageManager.Instance.currentPage as UI.UICopyRunMap;
                    page.Box(b.currentResource);
                }
            });
        }
    }
}